package cn.itcast.bos.service.system.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.system.RoleRepository;
import cn.itcast.bos.dao.system.UserRepository;
import cn.itcast.bos.domain.system.Role;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.UserSerivce;
@Service
@Transactional
public class UserServiceImpl implements UserSerivce {
	@Resource
	private UserRepository repository;
	@Resource
	private RoleRepository roleRepository; 
	@Override
	public User findByUsername(String username) {
		return repository.findByUsername(username);
	}
	@Override
	public List<User> findAll() {
		return repository.findAll();
	}
	@Override
	public void saveUser(User user, String[] roleIds) {
		if (roleIds != null) {
			for (String roleid : roleIds) {
				 Role role = roleRepository.findOne(Integer.parseInt(roleid));
				 user.getRoles().add(role);
			}
		}
		repository.save(user);
	}
}
